/*
 * CastDRAndroid
 * Copyright (c) 2014 Christian Holm Christensen
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 *
 */
package org.cholm.media.castdr.adapters;

import android.content.Context;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.example.android.imagedownloader.ImageDownloader;

import org.cholm.media.castdr.LogUtils;
import org.cholm.media.castdr.R;
import org.cholm.media.castdr.backend.Program;
import org.cholm.media.castdr.backend.Serie;

import java.util.List;

import static org.cholm.media.castdr.LogUtils.LOGd;
/**
 * Adapter to show the series of a sub-catogory
 *
 * @author cholm
 */
public class SubCategoryAdapter extends BaseExpandableListAdapter {
    /**
     * Debug tag
     */
    private static final String TAG = LogUtils.makeLogTag(SubCategoryAdapter.class);
    /**
     * Application context
     */
    private FragmentActivity mContext = null;
    /**
     * Our data
     */
    private List<Serie> mData = null;

    /**
     *
     */
    public SubCategoryAdapter(FragmentActivity context) {
        mContext = context;
    }

    /**
     * Get list of children for a given group
     *
     * @param groupPosition Group number
     * @return List of sub-categories
     */
    protected List<Program> getGroupList(int groupPosition) {
        Serie s = getSerie(groupPosition);
        if (s == null) return null;

        return s.getPrograms();
    }

    /**
     * Get the program at the given position
     *
     * @param groupPosition
     * @param childPosition
     * @return
     */
    public Program getProgram(int groupPosition, int childPosition) {
        Serie s = getSerie(groupPosition);
        if (s == null || s.getPrograms() == null
                || s.getPrograms().size() <= childPosition) {
            return null;
        }
        return s.getPrograms().get(childPosition);
    }

    /*
     * (non-Javadoc)
     *
     * @see android.widget.ExpandableListAdapter#getChild(int, int)
     */
    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return getProgram(groupPosition, childPosition);
    }

    /*
     * (non-Javadoc)
     *
     * @see android.widget.ExpandableListAdapter#getChildId(int, int)
     */
    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return getGroupId(groupPosition) | ((childPosition + 1) & 0xFFFF);
    }

    /*
     * (non-Javadoc)
     *
     * @see android.widget.ExpandableListAdapter#getChildView(int, int, boolean,
     * android.view.View, android.view.ViewGroup)
     */
    @Override
    public View getChildView(int groupPosition,
                             int childPosition,
                             boolean isLastChild,
                             View convertView,
                             ViewGroup parent) {
        final Program s = getProgram(groupPosition, childPosition);
        LOGd(TAG, "Get child view " + s.getTitle());
        ProgramHolder holder = null;
        // ListHolder holder = null;
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) this.mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.program_entry, null);
            // holder = new ListHolder(convertView);
            holder = new ProgramHolder(convertView);
        }
        if (holder == null) holder = (ProgramHolder) convertView.getTag();
        holder.set(s);

        return convertView;
    }

    /*
     * (non-Javadoc)
     *
     * @see android.widget.ExpandableListAdapter#getChildrenCount(int)
     */
    @Override
    public int getChildrenCount(int groupPosition) {
        Serie s = getSerie(groupPosition);
        if (s == null || s.getPrograms() == null) return 0;
        int ret = s.getPrograms().size();
        LOGd(TAG, "Serie " + s.getSlug() + " has " + ret + " child");
        return ret;
    }

    /*
     * (non-Javadoc)
     *
     * @see android.widget.ExpandableListAdapter#getGroup(int)
     */
    @Override
    public Object getGroup(int groupPosition) {
        if (mData == null) return null;

        return mData.get(groupPosition);
    }

    /**
     * Get the serie at a given location
     *
     * @param groupPosition
     * @return Serie object or null
     */
    public Serie getSerie(int groupPosition) {
        return (Serie) getGroup(groupPosition);
    }

    /*
     * (non-Javadoc)
     *
     * @see android.widget.ExpandableListAdapter#getGroupCount()
     */
    @Override
    public int getGroupCount() {
        if (mData == null) return 0;
        return mData.size();
    }

    /*
     * (non-Javadoc)
     *
     * @see android.widget.ExpandableListAdapter#getGroupId(int)
     */
    @Override
    public long getGroupId(int groupPosition) {
        return ((groupPosition + 1) & 0xFFFF) << 16;
    }

    /*
     * (non-Javadoc)
     *
     * @see android.widget.ExpandableListAdapter#getGroupView(int, boolean,
     * android.view.View, android.view.ViewGroup)
     */
    @Override
    public View getGroupView(int groupPosition,
                             boolean isExpanded,
                             View convertView,
                             ViewGroup parent) {
        // LOGd(TAG, "Looking from string w/id=" + groupPosition);
        Serie s = (Serie) getGroup(groupPosition);

        SerieHolder holder = null;
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) this.mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.serie_entry, null);
            holder = new SerieHolder(convertView);
        }
        if (holder == null) holder = (SerieHolder) convertView.getTag();
        holder.set(s);

        return convertView;
    }

    /*
     * (non-Javadoc)
     *
     * @see android.widget.ExpandableListAdapter#hasStableIds()
     */
    @Override
    public boolean hasStableIds() {
        return true;
    }

    /*
     * (non-Javadoc)
     *
     * @see android.widget.ExpandableListAdapter#isChildSelectable(int, int)
     *
     * Everything is select-able
     */
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    /**
     * Set the data of this adapter
     *
     * @param data The new data
     */
    public void setData(List<Serie> data) {
        mData = data;

    }

    /**
     * Holder class
     */
    public class SerieHolder {
        /**
         * Top view
         */
        public View mTopView = null;
        /**
         * Progress bar
         */
        public ProgressBar mProgress = null;
        /**
         * Text view
         */
        protected TextView mTitle = null;
        /**
         * Sub-title (Genre | Channel)
         */
        protected TextView mSubTitle = null;
        /**
         * Thumb
         */
        protected ImageView mImage = null;

        /**
         * Constructor
         *
         * @param v  View this is a holder for
         * @param id Id of text view
         */
        public SerieHolder(View v) {
            mTopView = v;
            mTitle = (TextView) v.findViewById(R.id.serie_title);
            mSubTitle = (TextView) v.findViewById(R.id.serie_subtitle);
            mImage = (ImageView) v.findViewById(R.id.serie_image);
            mProgress = (ProgressBar) v.findViewById(R.id.progress);
            v.setTag(this);
        }

        /**
         * Set fields of elements
         *
         * @param s Serie object
         */
        public void set(Serie s) {
            mTitle.setText(s.getTitle());
            mSubTitle.setText(s.getGenre() + " | " + s.getChannel());
            String url = s.getImage();
            ImageDownloader.instance().download(url, mImage);
        }

        /*
         * (non-Javadoc)
         *
         * @see java.lang.Object#toString()
         */
        public String toString() {
            return (String) mTitle.getText();
        }
    }

    /**
     * Holder class
     */
    public class ProgramHolder {
        /**
         * Top view
         */
        public View mTopView = null;
        /**
         * Text view
         */
        protected TextView mTitle = null;
        /**
         * Sub-title
         */
        protected TextView mSubTitle = null;

        /**
         * Constructor
         *
         * @param v  View this is a holder for
         */
        public ProgramHolder(View v) {
            mTopView = v;
            mTitle = (TextView) v.findViewById(R.id.program_title);
            mSubTitle = (TextView) v.findViewById(R.id.program_subtitle);
            v.setTag(this);
        }

        /**
         * Set the UI elements from the program
         *
         * @param s
         */
        public void set(Program s) {
            mTitle.setText(s.getTitle());
            mSubTitle.setText(s.getDate());
        }

        /*
         * (non-Javadoc)
         *
         * @see java.lang.Object#toString()
         */
        public String toString() {
            return (String) mTitle.getText();
        }
    }

}
